package com.collect.zambia.zhiScene.zhiRemind.zhiWhatsappService;

import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiWhatsapp.ZHIWhatsappAdjuster;
import com.collect.zambia.zhiBase.BaseFragment;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCustomerWhatsAppAccountFollow;
import com.collect.zambia.zhiBean.zhiSend.ZHISSendSmsEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiScene.zhiWidget.zhiSubmitRecTagView.ZHISubmitRecWhatsappView;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class CTWhatsappServiceFragment extends BaseFragment<CTWhatsappServicePresenter> implements CTWhatsappServiceContract.View {

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_query_count)
    TextView tv_query_count;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.iv_products)
    ImageView iv_products;

    private ZHISubmitRecWhatsappView mZHISubmitRecWhatsappView;

    ZHIWhatsappAdjuster mZHIWhatsappAdjuster = new ZHIWhatsappAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_whatsapp_service;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_products.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());

            mZHISubmitRecWhatsappView = new ZHISubmitRecWhatsappView(getActivity());
            mZHISubmitRecWhatsappView.setSubmitRecWhatsappItemClickListener(ctsCustomerWhatsAppAccountFollow -> {
                if (ctsCustomerWhatsAppAccountFollow != null && mPresenter != null) {
                    mPresenter.customerWhatsAppAccountFollow(getActivity(), ctsCustomerWhatsAppAccountFollow, true);
                }
            });

            initEvent();
            initData();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onCustomerWhatsAppAccountList(ArrayList<ZHIRCustomerWhatsAppAccountFollow> zhirCustomerWhatsAppAccountFollow) {
        try {
            srl_swipe_layout.setRefreshing(false);
            mZHIWhatsappAdjuster.setNewData(zhirCustomerWhatsAppAccountFollow);
            tv_page_title.setText("Total:" + zhirCustomerWhatsAppAccountFollow.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCustomerWhatsAppAccountFollow() {
        mZHISubmitRecWhatsappView.dismiss();
        initData();
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(this::initData);
        mZHIWhatsappAdjuster.setEmptyView(getRefreshEmptyView());
        rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_list.setAdapter(mZHIWhatsappAdjuster);

        mZHIWhatsappAdjuster.setZHIWhatsappAdjusterClickListener(this::showSendSmsView);
    }

    private void initData() {
        if (mPresenter != null) {
            mPresenter.customerWhatsAppAccountList(getActivity(), new ZHISSendSmsEntity(), true);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData();
        });
        return emptyView;
    }

    @SuppressLint("InflateParams")
    private void showSendSmsView(ZHIRCustomerWhatsAppAccountFollow zhirCustomerWhatsAppAccountFollow) {
        try {
            if (mZHISubmitRecWhatsappView == null) {
                mZHISubmitRecWhatsappView = new ZHISubmitRecWhatsappView(getActivity());
            }
            View parentView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_root_customer_service, null);
            mZHISubmitRecWhatsappView.setContact(zhirCustomerWhatsAppAccountFollow.getWhatsAppAccount());
            mZHISubmitRecWhatsappView.setOrderId(zhirCustomerWhatsAppAccountFollow.getOrderId());
            mZHISubmitRecWhatsappView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
